package com.movieknight;

import java.util.List;

import com.github.savvasdalkitsis.jtmdb.Movie;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
import android.view.KeyEvent;

// AddMovieSearchBrowser displays a search bar and provides methods relevant to searching.

public class AddMovieSearchBrowser extends Activity {
	private static final String LOG_TAG = "MovieSearchBrowser";
	EditText searchText;
	List<Movie> movies;
	MovieKnightApp app;
	Context context;
    static final int PICK_MOVIE_REQUEST = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Request a progress bar to be shown in the title.
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.movie_search);
		setProgressBarIndeterminateVisibility(true);
		
		// Configure Initial Options
		app = (MovieKnightApp) this.getApplication();
		context = getApplicationContext();
		Button searchMovieButton = (Button) findViewById(R.id.mainSearchButton);
		searchText = (EditText) findViewById(R.id.mainSearchText);
		searchText.setOnEditorActionListener(exampleListener);
		
		searchMovieButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				String searchString = searchText.getText().toString();
				if (searchString.length() < 1) {
					Toast.makeText(context, getString(R.string.mainEmptySearch), Toast.LENGTH_SHORT).show();
					Log.v(LOG_TAG, "NoSearchString!");
					return;
				}
				ProgressDialog dialog = new ProgressDialog(AddMovieSearchBrowser.this);
				dialog.setIcon(R.drawable.ic_launcher);
				dialog.setTitle(getString(R.string.mainPleaseWait));
				dialog.setMessage(getString(R.string.mainDownloading));
				dialog.show();
				MovieKnightApp.movieDownloader.searchMovie(searchString);
				dialog.dismiss();
				if (MovieKnightApp.movieDownloader.getLastMovieResult() != null) {
					Intent intent = new Intent(context, AddMovieListActivity.class);
					intent.putExtra("searchString", searchString);
					startActivityForResult(intent, PICK_MOVIE_REQUEST);
				} else {
					Toast.makeText(context, getString(R.string.mainNoResult),
							Toast.LENGTH_SHORT).show();
				}
			}

		});
		
	}
	
	TextView.OnEditorActionListener exampleListener = new TextView.OnEditorActionListener(){
		public boolean onEditorAction(TextView searchText, int actionId, KeyEvent event) {
			   if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) { 
				   String searchString = searchText.getText().toString();
					if (searchString.length() < 1) {
						Toast.makeText(context, getString(R.string.mainEmptySearch), Toast.LENGTH_SHORT).show();
						Log.v(LOG_TAG, "NoSearchString!");
						return true;
					}
					ProgressDialog dialog = new ProgressDialog(AddMovieSearchBrowser.this);
					dialog.setIcon(R.drawable.ic_launcher);
					dialog.setTitle(getString(R.string.mainPleaseWait));
					dialog.setMessage(getString(R.string.mainDownloading));
					dialog.show();
					MovieKnightApp.movieDownloader.searchMovie(searchString);
					dialog.dismiss();
					if (MovieKnightApp.movieDownloader.getLastMovieResult() != null) {
						Intent intent = new Intent(context, AddMovieListActivity.class);
						intent.putExtra("searchString", searchString);
						startActivityForResult(intent, PICK_MOVIE_REQUEST);
					} else {
						Toast.makeText(context, getString(R.string.mainNoResult),
								Toast.LENGTH_SHORT).show();
					}
			   }
			   return true;
			}
	};
	
	
	// Callback handler to stop progress bar
	Handler stopProgressHandler = new Handler() {
		public void handleMessage(Message m) {
			setProgressBarIndeterminateVisibility(false);
			switch (m.arg1) {
			case (TMDBMovieDownloader.STATUS_IO_ERROR): {
				Toast.makeText(context, getString(R.string.mainIoError),
						Toast.LENGTH_LONG).show();
				break;
			}
			case (TMDBMovieDownloader.STATUS_PARSE_ERROR): {
				Toast.makeText(context, getString(R.string.mainParseError),
						Toast.LENGTH_LONG).show();
				break;
			}
			case (TMDBMovieDownloader.STATUS_OK): {
				break;
			}
			}

		}
	};
	
	@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.v(LOG_TAG, Integer.toString(requestCode));
		super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_MOVIE_REQUEST) {
            if (resultCode == RESULT_OK) {
        		Intent data2 = new Intent();
        		data2.putExtra("returnKey1", data.getExtras().getInt("returnKey1"));
        		setResult(RESULT_OK, data2);
        		finish();
            }
        }
    }
	
}